package org.example.mall.admin.service.goods.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.example.mall.admin.mapper.goods.SpecParamsMapper;
import org.example.mall.admin.mapper.manage.SysLabelMapper;
import org.example.mall.admin.mapper.manage.SysTreeMapper;
import org.example.mall.admin.model.entity.goods.SpecParams;
import org.example.mall.admin.model.entity.manage.SysLabel;
import org.example.mall.admin.model.entity.manage.SysTree;
import org.example.mall.admin.model.enums.manage.LabelTypeEnum;
import org.example.mall.admin.model.vo.goods.GroupSpecParamsVo;
import org.example.mall.admin.service.goods.SpecParamsService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 规格模板值 服务实现类
 * </p>
 *
 * @author hzq
 * @since 2022-01-24
 */
@Service
@AllArgsConstructor
public class SpecParamsServiceImpl extends ServiceImpl<SpecParamsMapper, SpecParams> implements SpecParamsService {

    private final SysTreeMapper sysTreeMapper;
    private final SysLabelMapper sysLabelMapper;

    @Override
    public void delByGroupIds(List<Integer> groupIds) {
        if (CollUtil.isEmpty(groupIds))
            return;
        LambdaQueryWrapper<SpecParams> wrapper = Wrappers.<SpecParams>lambdaQuery()
                .in(SpecParams::getGroupId, groupIds);
        remove(wrapper);
    }

    @Override
    @Cacheable(value = "goods_group", key = "#categoryId  + '_spec'", unless = "#result.size() < 1")
    public List<GroupSpecParamsVo> groupSpecList(Integer categoryId) {
        List<GroupSpecParamsVo> result = new ArrayList<>();
        SysTree sysTree = sysTreeMapper.selectById(categoryId);
        LambdaQueryWrapper<SysLabel> labelWrapper = Wrappers.<SysLabel>lambdaQuery()
                .eq(SysLabel::getLabelType, LabelTypeEnum.GOODS_GROUP.getType())
                .eq(SysLabel::getExtra, categoryId)
                .orderByAsc(SysLabel::getSort);
        List<SysLabel> sysLabels = sysLabelMapper.selectList(labelWrapper);
        for (SysLabel label : sysLabels) {
            GroupSpecParamsVo groupVo = new GroupSpecParamsVo();
            groupVo.setCategoryId(categoryId);
            groupVo.setCategoryName(sysTree.getName());
            groupVo.setId(label.getId());
            groupVo.setName(label.getLabel());
            List<GroupSpecParamsVo.SpecParamsVo> genericParams = new ArrayList<>();
            List<GroupSpecParamsVo.SpecParamsVo> specialParams = new ArrayList<>();
            LambdaQueryWrapper<SpecParams> specWrapper = Wrappers.<SpecParams>lambdaQuery()
                    .eq(SpecParams::getGroupId, groupVo.getId()).orderByAsc(SpecParams::getSort);
            List<SpecParams> params = list(specWrapper);
            for (SpecParams param : params) {
                GroupSpecParamsVo.SpecParamsVo paramsVo = new GroupSpecParamsVo.SpecParamsVo();
                paramsVo.setGroupId(groupVo.getId());
                paramsVo.setGroupName(groupVo.getName());
                paramsVo.setGlobal(param.getGlobal());
                paramsVo.setId(param.getId());
                paramsVo.setName(param.getLabel());
                if (StrUtil.isNotBlank(param.getOptions())) {
                    paramsVo.setOptions(Arrays.asList(param.getOptions().split(StrPool.COMMA)));
                }
                // 全局属性
                if (paramsVo.getGlobal()) {
                    genericParams.add(paramsVo);
                }
                // 特殊属性
                else {
                    specialParams.add(paramsVo);
                }
            }
            groupVo.setGenericParams(genericParams);
            groupVo.setSpecialParams(specialParams);
            result.add(groupVo);
        }
        return result;
    }
}
